﻿define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 获取URL中的参数
            var urlParams = new URLSearchParams(location.search);
            var community_id = urlParams.get('community_id') || '';
            var publicwelfare_id = urlParams.get('publicwelfare_id') || '';
            
            // 如果在URL中没有找到参数，尝试从Config中获取
            if (!community_id && Config && Config.community_id) {
                community_id = Config.community_id;
            }
            if (!publicwelfare_id && Config && Config.publicwelfare_id) {
                publicwelfare_id = Config.publicwelfare_id;
            }
            
            // 调试输出参数
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'community/config/community/index' + location.search,
                    add_url: 'community/config/community/add' + location.search,
                    edit_url: 'community/config/community/edit',
                    del_url: 'community/config/community/del',
                    multi_url: 'community/config/community/multi',
                    import_url: 'community/config/community/import',
                    table: 'community_code_config',
                }
            });
            
            // 保存参数到全局变量，确保在对话框中也能访问
            window.communityConfigParams = {
                community_id: community_id,
                publicwelfare_id: publicwelfare_id
            };

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'weigh',
                pageSize: 2000,
                // 自定义表格操作按钮
                operate: function (value, row, index) {
                    var table = this.table;
                    // 默认按钮组
                    var buttons = $.extend([], this.buttons || []);
                    // 编辑按钮
                    buttons.push({
                        name: 'edit',
                        icon: 'fa fa-pencil',
                        title: __('Edit'),
                        extend: 'data-toggle="tooltip"',
                        classname: 'btn btn-xs btn-success btn-editone',
                        // 自定义URL，添加必要的参数
                        url: function (row) {
                            var params = [];
                            // 优先使用行数据中的参数，如果没有则使用URL中的参数
                            var rowCommunityId = row.community_id || community_id;
                            var rowPublicwelfareId = row.publicwelfare_id || publicwelfare_id;
                            
                            // 始终添加这两个参数，即使值为空
                            params.push('community_id=' + rowCommunityId);
                            params.push('publicwelfare_id=' + rowPublicwelfareId);
                            
                            // 检查是否有dialog参数
                            var dialogParam = location.search.match(/dialog=([^&]*)/);
                            if (dialogParam && dialogParam[1]) {
                                params.push('dialog=' + dialogParam[1]);
                            }
                            
                            var url = $.fn.bootstrapTable.defaults.extend.edit_url + '/' + row.id;
                            url += '?' + params.join('&');
                            console.log('Edit URL:', url); // 调试输出
                            return url;
                        }
                    });
                    return Table.api.buttonlink(this, buttons, value, row, index, 'operate');
                },
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'), sortable: true, visible: false},
                        {field: 'code', title: __('Code'), operate: 'LIKE'},
                        {field: 'name', title: '地点', operate: 'LIKE'},
                        {field: 'communityinfo.name', title: __('Community.name'), operate: 'LIKE'},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
            
            // 表格加载完成后，移除所有 .btn-dragsort 元素
            table.on('load-success.bs.table', function () {
                $('.btn-dragsort').remove();
            });
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            // 获取URL中的参数
            var urlParams = new URLSearchParams(location.search);
            var community_id = urlParams.get('community_id') || '';
            var publicwelfare_id = urlParams.get('publicwelfare_id') || '';
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
